package com.leetcode.LeetCode精选TOP面试题;

import java.util.HashSet;
import java.util.Set;

/**
 * @author 覃国强
 * @date 2022-06-23 18:07
 */
public class 题库_0128_最长连续序列 {

  public static void main(String[] args) {
    int[] nums = {0,3,7,2,5,8,4,6,0,1};
    Solution solution = new Solution();
    int longestConsecutive = solution.longestConsecutive(nums);
    System.out.println(longestConsecutive);
  }

  static
  class Solution {
    public int longestConsecutive(int[] nums) {
      if (nums == null || nums.length == 0) {
        return 0;
      }

      int answer = 0;
      Set<Integer> set = new HashSet<>();
      for (int j : nums) {
        set.add(j);
      }
      Set<Integer> mark = new HashSet<>();
      for (int i = 0; i < nums.length; ++i) {
        if (mark.contains(nums[i]) || mark.contains(nums[i] - 1)) {
          continue;
        }
        int length = 0;
        for (int num = nums[i]; set.contains(num); ++num) {
          mark.add(num);
          ++length;
        }
        answer = Math.max(length, answer);
      }

      return answer;
    }
  }

}
